<?php
(!defined('IN_SYSTEM') || !defined('ADMIN_PRE')) && exit('Access Denied!');
if ( !ican('page') ) msg('对不起，你没有权限执行本操作！');

$f = isset($_REQUEST['f'])?$_REQUEST['f']:'index';

if ( $f == 'index' ) {
	
	$page = max(1, getGP('page','G','int'));
	$pagesize = 20;
	$url = null;
	$offset = ($page - 1) * $pagesize;
	$num = $_G['db']->result("SELECT COUNT(*) AS num FROM ".DB_PREFIX."page");
	$sql = "SELECT * FROM ".DB_PREFIX."page ORDER BY orderid ASC LIMIT $offset, $pagesize";

	$result = $_G['db']->fetch_all($sql);
	include admintemplate('page');

} elseif ( $f == 'create' ) {

	$page = array('savetype'=>'add','orderid'=>0,'id'=>null, 'title'=>null,'pagename'=>null,'urlname'=>null,'ishide'=>null,'content'=>null,'keywords'=>null,'description'=>null);
	include admintemplate('page_create');

} elseif ( $f == 'edit' ) {
	
	$page = array();
	$page = $_G['db']->fetch_one_array("SELECT * FROM ".DB_PREFIX."page WHERE id = '".getGP('id','G','int')."'");
	$page['savetype'] = 'edit';
	if ( !$page ) msg('页面不存在'); 
	include admintemplate('page_create');

} elseif ( $f == 'save' ) {
	$id=getGP('id');
	$savetype=getGP('savetype');

	$_ENV = array();
	$_G['title'] = $_ENV['title'] = check_str(getGP('title','P'));
	$_ENV['pagename'] = check_str(getGP('pagename','P'));
	empty($_ENV['pagename']) && $_ENV['pagename'] = $_ENV['title'];
	$_ENV['urlname'] = getGP('urlname','P');
	$_ENV['urlname'] = preg_match('/[a-z0-9\-\_]*[a-z\-_]+[a-z0-9\-\_]*/i', $_ENV['urlname']) ? $_ENV['urlname'] : '';
	$_ENV['ishide'] = getGP('ishide','P','int');
	$_ENV['orderid'] = getGP('orderid','P','int');
	$_G['keywords'] = $_ENV['keywords'] = check_str(getGP('keywords','P'));
	$_G['description'] = $_ENV['description'] = check_str(getGP('description','P'));
	$_ENV['content'] = getGP('content','P');
	$_ENV['uid'] = $_G['uid'];
	$_ENV['username'] = $_G['username'];
	$_ENV['dateline'] = TIMESTAMP;

	if ( $savetype == 'add' ) {
		
		$id = $_G['db']->insert('page', $_ENV);

	} elseif ( $savetype == 'edit' ) {

		$oldfile = $_G['db']->result("SELECT filepath FROM ".DB_PREFIX."page WHERE id = '$id'");
		if ( file_exists(BASE_ROOT.$oldfile) ) {
			@unlink(BASE_ROOT.$oldfile);
		}
		$_G['db']->update('page', $_ENV, array('id'=>intval($id)));
	}
	save_alias($_ENV['urlname'],$id,'page');
	$_G['cache']->del($id,'page') ;
	msg('操作成功', 'admin.php?m=page');
	exit;

} elseif ( $f == 'delete' || $f == 'display' || $f == 'hide' ) {
	$idarr = getGP('id','P','array');
	if (count($idarr)) {
		$idstr = implode(',',$idarr);
		if ( $f == 'delete' ) {
			$result = $_G['db']->fetch_all("SELECT filepath FROM ".DB_PREFIX."page WHERE id IN ($idstr)");
			foreach ( $result as $row ) {
				@unlink(BASE_ROOT.$row['filepath']);
			}
			$_G['db']->query("DELETE FROM ".DB_PREFIX."page WHERE id IN ($idstr)");
		} else {
			$_G['db']->query("UPDATE ".DB_PREFIX."page SET ishide = ".($f == 'display' ? 0 : 1)." WHERE id IN ($idstr)");
		}
		foreach($idarr as $id){
			$_G['cache']->del($id,'page') ;
		}
	}
	msg('操作成功', 'admin.php?m=page');
	exit;
}
?>